/*
 * Copyright 2008 Jens Meiss
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package mtt.marks;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;

import mtt.marks.editor.MarkEditorController;
import mtt.marks.editor.events.ShowMarkEditorViewEvent;
import mtt.marks.events.RefreshMarksModelDataEvent;
import mtt.marks.events.RefreshMarksViewEvent;
import mtt.marks.events.ShowMarksViewEvent;
import mtt.menu.events.ShowMainMenuEvent;
import mtt.rms.dataObjects.Mark;
import mtt.rms.recordstores.marks.events.GetVectorMarksEvent;
import mtt.rms.recordstores.marks.events.RemoveMarkEvent;
import mtt.rms.recordstores.marks.events.SetVectorMarksEvent;
import hmvc.j2me.controller.AController;
import hmvc.j2me.events.IEvent;
import hmvc.j2me.events.ShowDisplayAbleEvent;
import hmvc.j2me.events.TerminateEvent;

/**
 * 
 * 
 * @author jens.meiss
 */
public class MarksController extends AController implements CommandListener {

	private MarksModel model = new MarksModel();
	
	private MarksView view = new MarksView();

	public MarksController() {
		
		setModel(new MarksModel());
		setView(new MarksView());
		
	}

	public void commandAction(Command command, Displayable d) {

		if (command == getView().getCommandBack()) {

			fireEventUp(new ShowMainMenuEvent());
			fireEvent(new TerminateEvent(), false, true, false);
		
		} else if (command == getView().getCommandAdd()) {

			addChildController(new MarkEditorController());
			fireEventDown(new ShowMarkEditorViewEvent());

		} else if (command == getView().getCommandEdit()) {

			int elementId = getView().getListMarks().getSelectedIndex();
			
			Mark mark = null;
			
			Enumeration enumeration = getModel().getHashtableItemIdMark().keys();
			while (enumeration.hasMoreElements()) {
				Integer selectedItemId = (Integer) enumeration.nextElement();
				
				if (selectedItemId.intValue() == elementId) {
					
					mark = (Mark) getModel().getHashtableItemIdMark().get(selectedItemId);
					break;
				}
			}
			
			addChildController(new MarkEditorController(mark));
			fireEventDown(new ShowMarkEditorViewEvent());

		} else if (command == getView().getCommandDelete()) {

			int elementId = getView().getListMarks().getSelectedIndex();
			Mark mark = (Mark) getModel().getHashtableItemIdMark().get(new Integer(elementId));

			fireEventUp(new RemoveMarkEvent(mark));
			fireEventUp(new GetVectorMarksEvent(this));

		}

	}
	
	private MarksModel getModel() {
		return model;
	}

	private MarksView getView() {
		return view;
	}

	public void init() {
		
		List listMarks = new List("Notenübersicht", List.IMPLICIT);
		listMarks.setCommandListener(this);
		getView().setListMarks(listMarks);
		
		Command commandBack = new Command("Hauptmenü", Command.BACK, 0);
		getView().setCommandBack(commandBack);
		
		Command commandAdd = new Command("Eintrag hinzufügen", Command.ITEM, 1);
		getView().setCommandAdd(commandAdd);
		
		Command commandEdit = new Command("Eintrag bearbeiten", Command.ITEM, 2);
		getView().setCommandEdit(commandEdit);
		
		Command commandDelete = new Command("Eintrag löschen", Command.ITEM, 2);
		getView().setCommandDelete(commandDelete);
		
		listMarks.addCommand(commandBack);
		listMarks.addCommand(commandAdd);
		
		register(RefreshMarksViewEvent.class);
		register(ShowMarksViewEvent.class);
		register(SetVectorMarksEvent.class);
		register(RefreshMarksModelDataEvent.class);
		
		
	}

	public void receiveEvent(IEvent event) {
		
		if (event instanceof ShowMarksViewEvent) {
			
			fireEventUp(new GetVectorMarksEvent(this));
			fireEventUp(new ShowDisplayAbleEvent(getView().getDisplayable()));
			
		} else if (event instanceof SetVectorMarksEvent) {
			
			refreshView((Vector) event.getPayload());
			
		} 
	}

	/**
	 * refreshes the view with the existing entries
	 * 
	 * @param vectorMarks
	 */
	private void refreshView(Vector vectorMarks) {

		getView().getListMarks().deleteAll();

		/*
		 * sind Einträge vorhanden
		 */
		if ((vectorMarks == null) || vectorMarks.size() == 0) {

			getView().getListMarks().append("Keine Noten eingetragen", null);
			
			getView().getListMarks().removeCommand(getView().getCommandDelete());
			getView().getListMarks().removeCommand(getView().getCommandEdit());

			if (vectorMarks == null) {
				/*
				 * leeren Vector erstellen
				 */
				vectorMarks = new Vector(3);
			}

		} else {

			getView().getListMarks().addCommand(getView().getCommandEdit());
			getView().getListMarks().addCommand(getView().getCommandDelete());

		}

		/*
		 * add standard commands
		 */
		getView().getListMarks().addCommand(getView().getCommandBack());
		getView().getListMarks().addCommand(getView().getCommandAdd());

		if (getModel().getHashtableItemIdMark() == null) {
			getModel().setHashtableItemIdMark(new Hashtable());
		}
		
		/*
		 * Einträge hinzufügen
		 */
		Enumeration enumeration = vectorMarks.elements();
		while (enumeration.hasMoreElements()) {
			
			Mark mark = (Mark) enumeration.nextElement();
			
			int id = getView().getListMarks().append("Name: " + mark.getName() + " Note: "
					+ mark.getMark(), null);
			
			getModel().getHashtableItemIdMark().put(new Integer(id), mark);
			
		}
	}

	private void setModel(MarksModel model) {
		this.model = model;
	}

	private void setView(MarksView view) {
		this.view = view;
	}

	public void terminate() {
	}

}
